<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center;background-color: #55dede">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 8px; overflow: hidden">
      <!--flex: 1 把div分成两半，一边一半-->
      <div style="flex: 1">
        <img src="@/assets/images/register.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
        <el-form :model="user" :rules="rules" ref="registerRef" style="width: 80%;"> <!-- 绑定user对象 -->
          <div style="font-size: 20px; font-weight: bold;text-align: center; margin-bottom: 30px">欢迎注册
          </div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" v-model="user.password" show-password></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" v-model="user.confirmPass" show-password></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 100%; background-color: #55dede" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已有账号？<span style="color: #0f9876; cursor: pointer" @click="$router.push('/login')">点击登录</span></div>
<!--            <div style="flex: 1; text-align: right"><span style="color: #0f9876; cursor: pointer">忘记密码</span></div>-->
          </div>
        </el-form>

      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from "@/components/ValidCode"; // 验证码模块

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    // 密码校验 // toLowerCase() 转换为小写
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password){
        callback(new Error('两次输入的密码不一致'))
      }else{
        callback();
      }
    }

    return {
      user: {
        username: '',
        password: '',
        confirmPass: '',  // 表单里二次确认的密码
      },
      rules: {
        username : { required: true, message: '请输入用户名', trigger: 'blur' } ,
        password : { required: true, message: '请输入密码', trigger: 'blur' } ,
        confirmPass: {  validator: validatePassword, trigger: 'blur' },
      }
    }
  },
  methods : {
    register(){
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          // 通过register接口，发送数据 this.user 得到数据 res
          this.$request.post("/register", this.user).then(res => {
            if(res.code == '200'){
              this.$router.push('/login')
              this.$message.success("注册成功")
            }else {
              this.$message.error(res.msg)
            }

          })
        }
      })
    }
  }
}
</script>

<style  scoped>
/*样式穿透*/
.el-input >>> .el-input__inner {
  height: 40px !important;
  line-height: 40px !important;
}
</style>